﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TechnicalIndicators.DateBased;
using TechnicalIndicators.Points;
using Utils;

namespace TechnicalIndicatorsIntegrationTests
{
	[TestClass]
	public class MovingAverageIT
	{
		[TestMethod]
		public void TestSMA1()
		{
			ObservableCollection2<DatedSeriePoint> source = new ObservableCollection2<DatedSeriePoint>();
			
			MovingAverage sma = new MovingAverage();
			sma.Source = new ReadWriteLockedObservableCollection<DatedSeriePoint>(source, new TimeSpan(0,0,1));

			sma.Configure(4);
			sma.Start();

			source.Add(new DatedSeriePoint(DateTime.Now, 1));
			Assert.IsNull(sma.LastValue);
			source.Add(new DatedSeriePoint(DateTime.Now, 2));
			Assert.IsNull(sma.LastValue);
			source.Add(new DatedSeriePoint(DateTime.Now, 3));
			Assert.IsNull(sma.LastValue);
			source.Add(new DatedSeriePoint(DateTime.Now, 4));
			Assert.IsNotNull(sma.LastValue);
			Assert.AreEqual(2.5m, sma.LastValue.Y);

			sma.Clear();
			sma.Start();

			Assert.IsNotNull(sma.LastValue);
			Assert.AreEqual(2.5m, sma.LastValue.Y);

			sma.Clear();
			sma.Configure(5);
			sma.Start();

			source.Add(new DatedSeriePoint(DateTime.Now, 5));
			source.Add(new DatedSeriePoint(DateTime.Now, 6));
			source.Add(new DatedSeriePoint(DateTime.Now, 7));
			source.Add(new DatedSeriePoint(DateTime.Now, 8));

			Assert.IsNotNull(sma.LastValue);
			Assert.AreEqual(6m, sma.LastValue.Y);

			Assert.AreEqual(4 ,sma.Serie.GetReadLock().Count);
			sma.Serie.FreeReadLock();

			Assert.AreEqual(5m, sma.Serie.GetReadLock()[2].Y);
			sma.Serie.FreeReadLock();

			sma.Clear();

			Assert.AreEqual(0, sma.Serie.GetReadLock().Count);
			sma.Serie.FreeReadLock();
			Assert.IsNull(sma.LastValue);
		}
	}
}
